home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2a.lha / p4-1.2a / monitors / addtwo.gs.c < prev    next >
C/C++ Source or Header  |  1992-10-19  |  1KB  |  71 lines

  1. #include "p4.h"
  2.  
  3. #define MAXLEN 500
  4. #define MAXPROCS 256
  5.  
  6.  
  7. struct globmem {
  8.     int length;
  9.     int a[MAXLEN], b[MAXLEN], c[MAXLEN];
  10.     int num_added[MAXPROCS];
  11.     p4_barrier_monitor_t barrier;
  12.     p4_getsub_monitor_t getsub;
  13. } *glob;
  14.  
  15.  
  16. slave()
  17. {
  18.     work();
  19. }
  20.  
  21. main(argc,argv)
  22. int  argc;
  23. char **argv;
  24. {
  25. int i, j, n;
  26.  
  27.     p4_initenv(&argc,argv);
  28.  
  29.     glob = (struct globmem *) p4_shmalloc(sizeof(struct globmem));
  30.  
  31.     p4_barrier_init(&(glob->barrier));
  32.     p4_getsub_init(&(glob->getsub));
  33.  
  34.     /* read in the length and the two vectors */
  35.     scanf("%d",&glob->length);
  36.     for (i = 0; i < glob->length; i++)
  37.     scanf("%d",&glob->a[i]);
  38.     for (i = 0; i < glob->length; i++)
  39.     scanf("%d",&glob->b[i]);
  40.     
  41.     p4_create_procgroup();
  42.     work();
  43.     /* print the answer */
  44.     for (i = 0; i < glob->length;) 
  45.     {
  46.     for (j = 0; (j < 9) && (i < glob->length); j++)
  47.         printf("%d\t", glob->c[i++]);
  48.     printf("\n");
  49.     }
  50.     for (i = 0, n = p4_num_total_ids(); i < n; i++)
  51.     printf("num by %d = %d \n",i,glob->num_added[i]);
  52.  
  53.     p4_wait_for_end();
  54. }
  55.  
  56. work()
  57. {
  58. int i, myid, nprocs;
  59.  
  60.     myid = p4_get_my_id();
  61.     glob->num_added[myid] = 0;
  62.     nprocs = p4_num_total_ids();
  63.     p4_barrier(&(glob->barrier),nprocs);
  64.     p4_getsub(&(glob->getsub),&i,glob->length - 1,nprocs);
  65.     while (i >= 0) {
  66.     glob->c[i] = glob->a[i] + glob->b[i];
  67.     glob->num_added[myid]++;
  68.     p4_getsub(&(glob->getsub),&i,glob->length - 1,nprocs);
  69.     }
  70. }
  71.